﻿/// <reference path="../jquery-1.9.1.min.js" />
/**
* Html5 Audio音频播放插件
* 1.针对Audio标签的封装，样式重构
* 2.本控件为JQuery控件
* 3.目前主题黑色
*/
(function ($) {
    function AudioMini(elem, opts) {
        //默认参数定义
        var defaults = {
            url: '',//歌曲地址
            name: '',//歌曲名称
            pic: '',//专辑封面
            singer: '',//歌手姓名
            volume: 100,//当前声音大小
            autoPlay: false,//是否自动播放
            loop: false,//是否循环播放
            type: 1,//控件样式类型，1-一般样式 2-简易样式 
            canFold: true,//指定简易模式下是否可折叠
            onPlay: function (audio) { },//当开始播放
            onEnded: function (audio) { },//当播放结束
        }
        this.elem = elem;
        this.opts = $.extend({}, defaults, opts);
    }
    AudioMini.prototype = {
        //初始化
        init: function () {
            var _this = this;
            var _opts = this.opts;
            //根据类型初始化
            if (_opts.type == 1) {
                _this.initCommon();
            } else if (_opts.type == 2) {
                _this.initSimple();
            }
        },
        //一般样式
        initCommon: function () {
            var _this = this;
            var _opts = this.opts;
            var _elem = this.elem;
            //
        },
        //初始换简单样式
        initSimple: function () {
            var _this = this;
            var _opts = this.opts;
            var _elem = this.elem;
            _elem.addClass('audio-simple');
            //追加icon
            _elem.append('<span class="iconaudio icon-yinfu"></span>');
            //绑定Audio标签
            var audio = $('<audio/>');
            var _audio = this.audio = audio.get(0);
            audio.attr('src', _opts.url);
            audio.volume = _opts.volume;
            audio.loop = _opts.loop;
            _elem.append(audio);
            _audio.onplay = function () {
                if (_opts.onPlay)
                    _opts.onPlay(_audio);
            }
            //播放结束
            _audio.onended = function () {
                pauseHandle();
                if (_opts.onEnded)
                    _opts.onEnded(_audio);
            }
            if (_opts.autoPlay) {
                playHandle();
            }
            //绑定事件
            _elem.click(function () {
                if (_elem.hasClass('audio-simple-play')) {
                    pauseHandle();
                } else {
                    playHandle();
                }
            });
            //播放处理
            function playHandle() {
                //播放样式
                _elem.addClass('audio-simple-play');
                _elem.attr('title', '点击暂停');
                //播放
                _this.play();
            }
            //暂停处理
            function pauseHandle() {
                //暂定样式
                _elem.removeClass('audio-simple-play');
                _elem.attr('title', '点击播放');
                //暂停
                _this.pause();
            }
        },
        //播放
        play: function () {
            var _this = this;
            var audio = _this.audio;
            audio.play();
        },
        //暂停
        pause: function () {
            var _this = this;
            var audio = _this.audio;
            audio.pause();
        },
        //销毁控件
        destroy: function () {
            var _this = this;
            _this.pause();
            _this.elem.remove();
        }
    }
    //获取div
    function getDivByClass(cla) {
        var div = $('<div />');
        div.addClass(cla);
        return div;
    }
    $.fn.audioMini = function (options) {
        var mini = new AudioMini(this, options);
        mini.init();
        return mini;
    }
})(jQuery);
